package com.Bigbluebook.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.Bigbluebook.R;
import com.Bigbluebook.entity.Message;

import java.util.List;
import java.util.Objects;

public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ChatMessageViewHolder> {
    private List<Message> messages;
    private Long userId; // 存储当前用户的ID


    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public ChatMessageAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @NonNull
    @Override
    public ChatMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
        return new ChatMessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatMessageViewHolder holder, int position) {
        Message message = messages.get(position);

        // 设置消息内容
        holder.leftMsgTextView.setText(message.getContent());
        Log.e("用户信息","当前发送id是"+ message.getSenderId() + "userid是" + userId);
        holder.rightMsgTextView.setText(message.getContent());

        // 根据发送者ID设置布局
        if (Objects.equals(message.getSenderId(), userId)) {
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);

            // 设置右侧消息布局的数据
        } else {
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);

            // 设置左侧消息布局的数据
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public static class ChatMessageViewHolder extends RecyclerView.ViewHolder {
        TextView leftMsgTextView;
        TextView rightMsgTextView;
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        public ChatMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            leftMsgTextView = itemView.findViewById(R.id.left_msg);
            rightMsgTextView = itemView.findViewById(R.id.right_msg);
            leftLayout = itemView.findViewById(R.id.left_layout);
            rightLayout = itemView.findViewById(R.id.right_layout);
        }
    }
}